﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using CPPProject.CppModel;
using CPPProject.CppServices.Dto;
using Microsoft.EntityFrameworkCore;
using OplugAbpProject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace CPPProject.CppServices
{
    public class T_Biz_ItemQRAppService : AsyncCrudAppService<T_Biz_ItemQR, T_Biz_ItemQRDto, Guid, PagedResultRequestDto, UpdateT_Biz_ItemQRDto, UpdateT_Biz_ItemQRDto>, IT_Biz_ItemQRAppService
    {
        private readonly IObjectMapper _objectMapper;
        public T_Biz_ItemQRAppService(IRepository<T_Biz_ItemQR, Guid> repository, IObjectMapper objectMapper) : base(repository)
        {
            _objectMapper = objectMapper;
        }
        #region 个体资产
        /// <summary>
        /// 个体资产
        /// </summary>
        /// <param name="nCompanyID">公司ID</param>
        /// <param name="ItemCode">资产编码</param>
        /// <param name="skipCount"></param>
        /// <param name="maxResultCount"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<T_Biz_ItemQRDto>> GetList(string nCompanyID, string ItemCode = "", int skipCount = 0, int maxResultCount = 100)
        {
            PagedResultDto<T_Biz_ItemQRDto> resultDto = new PagedResultDto<T_Biz_ItemQRDto>();
            List<T_Biz_ItemQR> lists = new List<T_Biz_ItemQR>();
            Expression<Func<T_Biz_ItemQR, bool>> expr = o => o.NCompanyID == nCompanyID;
            if (!string.IsNullOrEmpty(ItemCode))
            {
                expr = expr.ExpressionAnd(o => o.CItemCode == ItemCode);
            }
            lists = await Repository.GetAll().Where(expr).ToListAsync();
            var dtos = lists.Skip(skipCount).Take(maxResultCount).ToList();
            resultDto.Items = _objectMapper.Map<List<T_Biz_ItemQRDto>>(dtos);
            resultDto.TotalCount = lists.Count;
            return resultDto;
        }
        #endregion

        #region 获取起始序号
        /// <summary>
        /// 获取起始序号
        /// </summary>
        /// <param name="nCompanyID">公司ID</param>
        /// <param name="ItemCode">资产编码</param>
        /// <returns></returns>
        public async Task<decimal> GetPrintLoad(string nCompanyID, string ItemCode)
        {
            var nSeq = await Repository.GetAll().Where(it => it.NCompanyID == nCompanyID && it.CItemCode == ItemCode).Select(it => it.NSeq).MaxAsync();
            return nSeq != null ? nSeq.Value + 1 : 1;
        }
        #endregion
    }
}
